﻿Imports IICModel.ConversionObjects
Imports IICModel.BusObjects
Imports IICModel.ConversionObjects.Mappers
Imports IICModel.BusObjects.Mappers


Namespace DataConbversionLogic
    Public Class MEMBERSConverter
        Inherits BaseDataConverter

        Protected Overrides Sub mapData(ByVal source As org.model.lib.Model.IModelObject, _
                                        ByRef destination As org.model.lib.Model.IModelObject)

            Dim s As MEMBERS = CType(source, MEMBERS)
            Dim d As Student = CType(destination, Student)

            d.MemberNumber = s.MemberNo
            Dim lname As String = Trim(s.Name1) & Trim(s.Name2)
            'If Trim(s.Name1) = "ΧΑΤΖΗΧΡΥΣΑΝΘΟΥ ΛΕΥΤΕ" Then
            '    Stop
            'End If
            d.FirstName = parseFirstName(lname)
            d.LastName = parseLastName(lname)

            If String.IsNullOrEmpty(d.FirstName) Then
                Me.logMsg(s.Id & " skipped, FirstName is empty ")
                destination = Nothing
                Exit Sub
            End If
            If String.IsNullOrEmpty(d.LastName) Then
                Me.logMsg(s.Id & " skipped, LastName is empty ")
                destination = Nothing
                Exit Sub
            End If

            d.DateOfBirth = s.BirthDt
            d.Company = Trim(s.COMPANY)

            If Not String.IsNullOrEmpty(s.MemberIc) _
                    AndAlso s.MemberIc.Trim <> "" _
                    AndAlso s.MemberIc.Trim <> "0" Then

                d.IdNumber = Trim(s.MemberIc)
            Else
                d.IdNumber = Nothing
            End If

            d.HomePhone = Trim(s.PhoneRes)
            d.OfficePhone = Trim(s.PhoneWrk)
            d.InformCompanyFlag = True
            d.Address1 = Trim(s.Address1)
            d.Address2 = Trim(s.Address2)
            d.Address3 = Trim(s.TOWN)
            d.Address4 = Trim(s.PostCode)
            d.IsMember = True
            d.CountryId = 1
            d.LastPaymentDate = s.LastPayDt
            d.LastSubscriptionYear = s.LastYrPaid
            d.MemberSince = s.AdmDt
            d.IsMemberActive = CType(IIf(Trim(s.STATUS) = "I", 0, 1), Integer?)
            d.DataConversionSourceTable = getTableName()

            If s.DISTRICT.HasValue AndAlso s.DISTRICT.GetValueOrDefault > 0 Then
                d.DistrictId = s.DISTRICT
            End If

            'If Trim(d.IdNumber) = "0" Then Stop

            If String.IsNullOrEmpty(d.IdNumber) = False Then
                If StudentDataUtils.findOne("IdNumber={0}", _
                                                   d.IdNumber) IsNot Nothing Then

                    Me.logMsgFormat(s.Id & ":Member with ID {0} already exists.  Source Name: {1}, Target name: {2}", _
                                    CStr(d.IdNumber), s.Name1, d.StudentFullName)

                    destination = Nothing

                End If
            End If

        End Sub
        Protected Overrides Function getTableName() As String
            Return "MEMBERS"
        End Function
        Protected Overrides Sub instantiateMappers()

            Me.dataConvMapper = New MEMBERSDBMapper()
            Me.newdbMapper = New StudentDBMapper

        End Sub

        Protected Overrides Function checkData(ByVal source As IModelObject, _
                                  ByVal destination As IModelObject) As Boolean

            Dim ret As Boolean = True
            Dim lSource As MEMBERS = CType(source, MEMBERS)

            If StudentDataUtils.findOne("membernumber={0}", _
                                                lSource.MemberNo) IsNot Nothing Then

                Me.logMsgFormat("MemberNumber {0} already exists.  Source Name: {1}, Target name: {2}", _
                                CStr(lSource.MemberNo), lSource.Name1, CType(destination, Student).StudentFullName)
                ret = False

            End If

            Return ret

        End Function

        Public Shared Function parseFirstName(ByVal p1 As String) As String

            Dim lP1IndexOf As Integer = p1.IndexOf(" ")

            If lP1IndexOf > 0 Then
                Return Trim(p1.Substring(lP1IndexOf))
            End If
            Return String.Empty

        End Function

        Public Shared Function parseLastName(ByVal p1 As String) As String
            Dim lP1IndexOf As Integer = p1.IndexOf(" ")

            If lP1IndexOf > 0 Then
                Return Trim(p1.Substring(0, lP1IndexOf))
            End If
            Return String.Empty
        End Function


    End Class
End Namespace